from django.db import models
import datetime
from django.utils import timezone
# Create your models here.
# 权限类型（角色）
# name varchar(30)  '权限类型的名称'
class PermissionType(models.Model):
    """权限的类型"""
    name = models.CharField(max_length=30, verbose_name='权限类型的名称')

    class Meta:
        db_table = 'permission_type'

    def __str__(self):
        return self.name


class User(models.Model):
    username = models.CharField(max_length=30,verbose_name='用户名')
    phone = models.CharField(max_length=11,verbose_name='手机号')
    password = models.CharField(max_length=256,verbose_name='密码')
    created_time = models.DateTimeField(verbose_name='注册时间',default=timezone.now)
    last_login = models.DateTimeField(verbose_name='登录时间',default=timezone.now)
    email = models.EmailField(blank=True ,verbose_name='邮箱')
    is_staff = models.BooleanField(verbose_name='实否是管理员',default=False)
    is_activated = models.BooleanField(verbose_name='是否激活权限',default=True)
    role = models.ManyToManyField(PermissionType, verbose_name='所属角色')
    class Meta:
        verbose_name_plural = '用户表'
        db_table = 'tb_user'
    def __str__(self):
        return self.username


# 权限表
# name  varchar(30)  '权限名称'
# code_name  varchar(300)  '权限识别名称'
# type_name fk  --->PermissionType
class Permission(models.Model):
    """
    权限模型类
    """
    name = models.CharField(max_length=30, verbose_name='权限名称')
    code_name = models.CharField(max_length=300, verbose_name='权限识别名称')
    type_name = models.ForeignKey(PermissionType, on_delete=models.CASCADE)
    class Meta:
        db_table = 'permission'
    def __str__(self):
        return self.name



# 用户组
# name  varchar(30)  '组名称'
# user  m:n
# permission m:n
class UserGroup(models.Model):
    """用户组的模型类"""
    name = models.CharField(max_length=30, verbose_name='组名称')
    user = models.ManyToManyField(User)
    # 更改模型类: 改为多对多
    permission = models.ManyToManyField(Permission)

    class Meta:
        db_table = 'user_group'

    def __str__(self):
        return self.name